//
//  HEApplyCirlceViewController.m
//  HealthFamily
//
//  Created by 吴欧 on 2021/3/2.
//  Copyright © 2021 honglutec. All rights reserved.
//

#import "HEApplyCirlceViewController.h"
#import "HERoomApplyModel.h"
#import "BRStringPickerView.h"

@interface HEApplyCirlceViewController ()<QMUITextViewDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;
// 约束参照视图,也是容器视图
@property (nonatomic, strong) UIView *contentView;

@property (nonatomic, strong) UILabel *titleLb;
@property (nonatomic, strong) UILabel *resLb;
@property (nonatomic, strong) QMUIGhostButton *selectBtn;
@property (nonatomic, strong) QMUITextView *textView;
@property (nonatomic, strong) NSMutableArray *itemArr;


@property (nonatomic, strong) NSString *theme;

@end

@implementation HEApplyCirlceViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"申请圈子";
    
    [self loaditemData];
    [self initUI];
}

- (void)initUI {
    
    // 1. 添加scrollView
    [self.view addSubview:self.scrollView];
    // 2. 添加参照视图
    [self.scrollView addSubview:self.contentView];
    
   
    _titleLb = [UILabel new];
    _titleLb.text = @"圈子主题";
    _titleLb.font = DYBlodFont(17);
    _titleLb.textColor = CNavLightBlackColor;
    [_contentView addSubview: _titleLb];
     
    // 选择主题
    _selectBtn = [[QMUIGhostButton alloc] initWithGhostColor: UIColorPlaceholder];
    _selectBtn.imagePosition = QMUIButtonImagePositionRight;
    _selectBtn.spacingBetweenImageAndTitle = 15;
    _selectBtn.titleLabel.font = UIFontMake(16);
    _selectBtn.titleLabel.textColor = CNavLightBlackColor;
    [_selectBtn setTitle:@"请为圈子选一个主题" forState:UIControlStateNormal];
    [_selectBtn setImage:IMAGE_NAMED(@"item_expand") forState:UIControlStateNormal];
    [_selectBtn setTitleColor:CNavLightBlackColor forState:UIControlStateNormal];
    [_contentView addSubview:_selectBtn];
    [_selectBtn addTarget:self action:@selector(popSelectView) forControlEvents:UIControlEventTouchUpInside];
     
    _resLb = [UILabel new];
    _resLb.text = @"申请原因";
    _resLb.font = DYBlodFont(17);
    _resLb.textColor = CNavLightBlackColor;
    [_contentView addSubview: _resLb];
    
    
    // 描述
    _textView = [QMUITextView new];
    // 限制可输入的字符长度
    self.textView.maximumTextLength = 15000;
    self.textView.placeholder = @"请输入您为何要申请开通该类房间。描述越详细通过的几率会更大哦！感谢您的支持！";
    self.textView.placeholderColor = UIColorPlaceholder; // 自定义 placeholder 的颜色
    self.textView.textContainerInset = UIEdgeInsetsMake(5, 7, 5, 7);
    self.textView.typingAttributes = @{NSFontAttributeName: DYFont(16),
                                       NSParagraphStyleAttributeName: [NSMutableParagraphStyle qmui_paragraphStyleWithLineHeight:20]
    };
    _textView.backgroundColor = CSplitLineColor;
    _textView.delegate = self;
    self.textView.layer.cornerRadius = 4;
    [self.contentView addSubview:_textView];
    
    
    [_titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_contentView).offset(20);
        make.left.equalTo(_contentView).offset(15);
        make.right.equalTo(_contentView).offset(-15);
    }];
    
    [_selectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_titleLb.mas_bottom).offset(20);
        make.left.equalTo(_contentView).offset(15);
        make.right.equalTo(_contentView).offset(-15);
        make.height.offset(45);
    }];
    
    [_resLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_selectBtn.mas_bottom).offset(20);
        make.left.equalTo(_contentView).offset(15);
        make.right.equalTo(_contentView).offset(-15);
    }];
    
    [_textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_resLb.mas_bottom).mas_offset(10);
        make.left.equalTo(_contentView).mas_offset(15);
        make.right.equalTo(_contentView).mas_offset(-15);
        make.height.mas_greaterThanOrEqualTo(180).priority(UILayoutPriorityRequired);
    }];
    
     
    // 提交
    QMUIFillButton *submitBtn = [[QMUIFillButton alloc] initWithFillColor:CNavBgColor titleTextColor:CNavBgFontColor];
    submitBtn.titleLabel.font = UIFontMake(16);
    [submitBtn setTitle:@"提交" forState:UIControlStateNormal];
    [_contentView addSubview:submitBtn];
    [submitBtn addTarget:self action:@selector(submitApply) forControlEvents:UIControlEventTouchUpInside];


    [submitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_textView.mas_bottom).mas_offset(30);
        make.centerX.equalTo(self.view);
        make.left.equalTo(_contentView).mas_offset(40);
        make.right.equalTo(_contentView).mas_offset(-40);
        make.height.mas_offset(40);
        make.bottom.equalTo(_contentView).mas_offset(-30);
    }];
}

- (void)popSelectView {
    [_textView resignFirstResponder];
    
    // 性别
    BRStringPickerView *stringPickerView = [[BRStringPickerView alloc]init];
    stringPickerView.pickerMode = BRStringPickerComponentSingle;
    stringPickerView.title = @"圈子主题";
    stringPickerView.dataSourceArr = self.itemArr;
    stringPickerView.selectValue = @"";
    
    KWeakSelf
    stringPickerView.resultModelBlock = ^(BRResultModel * _Nullable resultModel) {
        [weakSelf.selectBtn setTitle:resultModel.value forState:UIControlStateNormal];
        weakSelf.theme = resultModel.value;
    };
    
    // 自定义弹框样式
    BRPickerStyle *customStyle = [BRPickerStyle pickerStyleWithThemeColor:CNavBgColor];
    stringPickerView.pickerStyle = customStyle;
    [stringPickerView show];
}

- (void)loaditemData {
    _itemArr = @[].mutableCopy;
    [WOAFNetWorkingHandle GETWithBaseUrl:KServerUrl suffixUrl:KRoomApplyItem parameters:@{} success:^(id  _Nonnull responseObject) {
        HEArrayResponse *response = [HEArrayResponse mj_objectWithKeyValues:responseObject];
        NSArray *arr = [HERoomApplyModel mj_objectArrayWithKeyValuesArray:response.data];
        
        for (HERoomApplyModel *item in arr) {
            [self.itemArr addObject: item.item_text];
        }
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

- (void)submitApply {
    if ([DYTools isBlankString:self.theme]) {
        [Dialog toastBottom:@"请选择一个主题!"];
        return;
    }
    
    if ([DYTools isBlankString: self.textView.text]) {
        [Dialog toastBottom:@"请填写申请原因!"];
        return;
    }
    
    NSMutableDictionary *dict = @{}.mutableCopy;
    [dict setValue:self.theme forKey:@"applyTitle"];
    [dict setValue:self.textView.text forKey:@"apply_note"];

    [WOAFNetWorkingHandle POSTWithBaseUrl:KServerUrl suffixUrl:KRoomSaveApply parameters:dict success:^(id  _Nonnull responseObject) {
        HEResponse *response = [HEResponse mj_objectWithKeyValues:responseObject];

        if (response.success) {
            [Dialog toastBottom:@"已提交申请"];
            if (self.backBlock) {
                self.backBlock();
            }
            [self.navigationController popViewControllerAnimated:YES];
        }
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

#pragma mark - QMUITextViewDelegate
// 实现这个 delegate 方法就可以实现自增高
- (void)textView:(QMUITextView *)textView newHeightAfterTextChanged:(CGFloat)height {
    [_textView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.offset(height).priority(UILayoutPriorityDefaultLow);
    }];
}

- (void)textView:(QMUITextView *)textView didPreventTextChangeInRange:(NSRange)range replacementText:(NSString *)replacementText {
    [Dialog toast:[NSString stringWithFormat:@"文字不能超过 %@ 个字符", @(textView.maximumTextLength)] delay:2.0];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    // 布局子控件
    // 设置scrollView约束
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(ZXNavBarHeight, 0, 0, 0));
    }];
    
    // 设置参照视图的约束
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.scrollView);
        make.width.equalTo(self.scrollView);
//        make.height.greaterThanOrEqualTo(@0.0f);
    }];
}

#pragma mark - getters
// scrollView
- (UIScrollView *)scrollView {
    if (_scrollView == nil) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.backgroundColor = KWhiteColor;
        _scrollView.alwaysBounceVertical = YES;
        _scrollView.bounces = YES;
    }
    return _scrollView;
}

// 约束参照视图
- (UIView *)contentView {
    if (_contentView == nil) {
        _contentView = [[UIView alloc] init];
        _contentView.backgroundColor = KWhiteColor;
    }
    return _contentView;
}

@end
